package org.pine.dictionary.util;

import com.alibaba.excel.converters.date.DateStringConverter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.text.ParseException;
import java.util.Date;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * <p>
 * 时间-表格字段转换器
 * </p>
 *
 * @author pine
 * @since 2024-05-16
 */
public class DateExcelConverter extends DateStringConverter {
    public DateExcelConverter() {
    }

    public Date convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws ParseException {
        return contentProperty != null && contentProperty.getDateTimeFormatProperty() != null ? DateUtils.str2Date(cellData.getStringValue(), contentProperty.getDateTimeFormatProperty().getFormat(), LocaleContextHolder.getTimeZone()) : DateUtils.str2Date(cellData.getStringValue(), LocaleContextHolder.getTimeZone());
    }

    public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        return contentProperty != null && contentProperty.getDateTimeFormatProperty() != null ? new WriteCellData<>(DateUtils.date2Str(value, contentProperty.getDateTimeFormatProperty().getFormat(), LocaleContextHolder.getTimeZone())) : new WriteCellData<>(DateUtils.date2Str(value, true, LocaleContextHolder.getTimeZone()));
    }
}
